﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MaxStudio.Concrete.DBUtility;
using MaxStudio.Concrete.Server.Dao;
using MaxStudio.Concrete.Server.Interface; 

namespace MaxStudio.Concrete.Server.Service
{
    public class MasterDataService : ServiceHandle ,IMasterData
    {

        #region IMasterData Members

        public OperationResult<List<tb_MasterData>> GetMasterDataByTypeId(int typeId)
        {
            ConcreteDaoDataContext dataContext = getDateContext();

            OperationResult<List<tb_MasterData>> result = new OperationResult<List<tb_MasterData>>();
            List<tb_MasterData> list = new List<tb_MasterData>();
            try
            {
                list = dataContext.GetMasterDataByTypeId(typeId);

                if (list == null)
                {
                    result.errorMessageKey = "Error";
                }
                else
                {
                    result.obj = list;
                    result.operationSuccess = true;
                }
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);

            }
            return result;
        }

        public OperationResult<tb_MasterData> GetMasterDateById(int id)
        {
            ConcreteDaoDataContext dataContext = getDateContext();

            OperationResult< tb_MasterData>  result = new OperationResult< tb_MasterData>();
         
            try
            {
                tb_MasterData entity = new tb_MasterData();
                entity = dataContext.GetMasterDateById(id);

                if (entity == null)
                {
                    result.errorMessageKey = "Error";
                }
                else
                {
                    result.obj = entity;
                    result.operationSuccess = true;
                }
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);

            }
            return result;
        }

        public OperationResult<tb_MasterData> CreateMasterData(tb_MasterData entity)
        {
            OperationResult<tb_MasterData> result = new OperationResult<tb_MasterData>();
            ConcreteDaoDataContext dataContext = getDateContext();

            try
            {
                dataContext.CreateMasterData(entity);
                dataContext.SubmitChanges();
                result.obj = entity;
                result.operationSuccess = true;
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);
            }

            return result;
        }

        public OperationResult<tb_MasterData> ModifyMasterData(tb_MasterData entity)
        {
            OperationResult<tb_MasterData> result = new OperationResult<tb_MasterData>();
            ConcreteDaoDataContext dataContext = getDateContext();

            try
            {
                dataContext.ModifyMasterData(entity);
                dataContext.SubmitChanges();
                result.obj = entity;
                result.operationSuccess = true;
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);
            }

            return result;
        }

        public OperationResult<tb_MasterData> DeleteMasterData(tb_MasterData entity)
        {
            OperationResult<tb_MasterData> result = new OperationResult<tb_MasterData>();
            ConcreteDaoDataContext dataContext = getDateContext();

            try
            {
                dataContext.DeleteMasterData(entity);
                dataContext.SubmitChanges();
                result.obj = entity;
                result.operationSuccess = true;
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);
            }

            return result;
        }

        #endregion
    }
}
